package cn.itsource.org.controller;

import cn.itsource.basic.util.AjaxResult;
import cn.itsource.org.domain.Shop;
import cn.itsource.org.service.ShopService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

@RestController
@RequestMapping("/shop")
@Api(tags = "店铺管理接口")
public class ShopController {
    @Autowired
    private ShopService shopService;
    @PostMapping("/settlement")
    @ApiOperation(value = "店铺入驻")
    public AjaxResult settlement(@Valid @RequestBody Shop shop){

        return shopService.settlement(shop);
    }

    @PostMapping
    @ApiOperation(value = "新增修改部门，新增没有id，修改有id") //swagger API
    public AjaxResult save(@RequestBody Shop shop){
        try {
            if(shop.getId()==null){
                shopService.save(shop);
            }else {
                shopService.update(shop);
            }
            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }
    }

    @ApiOperation(value = "根据id删除部门对象") //swagger API
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id") Long id){

        try {
            shopService.delete(id);

            return AjaxResult.createSuccess();
        } catch (Exception e) {
            //打印错误的堆栈信息
            e.printStackTrace();
            return AjaxResult.createError();
        }

    }

    @GetMapping("/findManager")
    @ApiOperation(value = "查询所有的经理")
    public AjaxResult findManager(){
        List<Shop> all = shopService.findAll();
        return AjaxResult.createSuccess(all);
    }


}
